package pcb.views 
{
	import com.intel.core.UIComponent;
	import com.intel.utils.Debugger;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import pcb.models.LocaleModel;
	import pcb.views.uievents.UIEvent;
	
	/**
	 * ...
	 * @author Fintan Boyle
	 */
	public class LegalScreen extends UIComponent implements ILocalise 
	{
		private var context:LoaderContext;
		private var alertManager:MovieClip;
		private var showEula:Boolean;
		private var eulaScreen:MovieClip;
		private var messageScreen:MovieClip;
		private var legTxt:String;
		
		public function LegalScreen() 
		{
			context = new LoaderContext();  
			context.applicationDomain = ApplicationDomain.currentDomain; 
		}
		
		public function init(showEula:Boolean):void 
		{
			this.showEula = showEula;
			
			displayEulaDialog();
			
			//loadAlertManagerRSL();
		}
		
		public function message():void 
		{
			makeBg();
			
			messageScreen = new MessageMc();
			
			messageScreen.messageTxt.text = "This software will ONLY run on PCs with an Intel processor.";
			
			messageScreen.x = this.stage.stageWidth / 2 - messageScreen.width / 2;
			messageScreen.y = this.stage.stageHeight / 2 - messageScreen.height / 2;
			
			addChild(messageScreen);
		}
		
		
		public function displayEulaDialog():void
		{
			makeBg();
			
			legTxt = "By downloading or using this software, you agree to these terms. If you do not agree to them, do not use or download the software. \n\n";
			legTxt += "This software is copyrighted and any unauthorized use of these materials may violate copyright, trademark, and other laws. \n\n";
			legTxt += "This training is made available to you by Intel without cost or license fee.\n\n";
			legTxt += "You may not: (a) modify the Materials or use them for any other purpose than for your personal use for educational purposes on your own PC; (b) decompile, reverse engineer, or disassemble software Materials except and only to the extent permitted by applicable law; (c) remove any copyright or other proprietary notices from the Materials; (d) transfer the Materials to another person. You agree to prevent any unauthorized copying of the Materials.\n\n";
			legTxt += "THE MATERIALS ARE PROVIDED 'AS IS' WITHOUT ANY EXPRESS OR IMPLIED WARRANTY OF ANY KIND INCLUDING WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT OF INTELLECTUAL PROPERTY, OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT SHALL INTEL OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, LOSS OF INFORMATION) ARISING OUT OF THE USE OF OR INABILITY TO USE THE MATERIALS, EVEN IF INTEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. BECAUSE SOME JURISDICTIONS PROHIBIT THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO YOU.\n\n";
			legTxt += "Intel Corporation and its suppliers further do not warrant the accuracy or completeness of the information, text, graphics, links or other items contained within these materials. Intel Corporation may make changes to these materials, or to the products described therein, at any time without notice. Intel Corporation makes no commitment to update the Materials.";
			legTxt += "For technical issues with the software please seek support from the manufacturer or store where the PC was purchased. This software will ONLY run on PCs with an Intel processor.";
			legTxt += "Your system integrator may or may not have installed the Microsoft® Office Suite of Products on your computer.";
			legTxt += "Hyper-Threading Technology requires a computer system with an Intel® Pentium® 4 processor supporting HT Technology and a Hyper-Threading Technology enabled chipset, BIOS and operating system. Performance will vary depending on the specific hardware and software you use. See http://www.intel.com/info/hyperthreading/ for more information including details on which processors support HT Technology.";
			legTxt += "Intel®, Intel Inside®, Pentium®, Intel® Centrino™, Intel® Celeron®, the Intel® logo and the Intel Inside® logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other countries.";
			legTxt += "*Other names and brands may be claimed as the property of others.";

			eulaScreen = new EulaMc();
			eulaScreen.eulaTxt.setStyle("upSkin",Sprite);
			eulaScreen.eulaTxt.setStyle("focusRectSkin",Sprite);
			eulaScreen.eulaTitle.text = "Intel Corporation Customer Legal Agreement";
			eulaScreen.eulaTxt.text = legTxt;
			eulaScreen.but_disagree.txt.text = "Disagree";
			eulaScreen.but_agree.txt.text = "Agree";
			eulaScreen.but_disagree.addEventListener(MouseEvent.CLICK, onDisagree);
			eulaScreen.but_agree.addEventListener(MouseEvent.CLICK, onAgree);
			
			
			eulaScreen.x = this.stage.stageWidth / 2 - eulaScreen.width / 2;
			eulaScreen.y = this.stage.stageHeight / 2 - eulaScreen.height / 2;
			addChild(eulaScreen);
		}
		
		private function makeBg():void 
		{
			this.graphics.beginFill(0xffffff);
			this.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
			this.graphics.endFill();
		}
		
		private function onAgree(e:MouseEvent):void 
		{
			agreeBut_onClick();
			
			e.target.removeEventListener(MouseEvent.CLICK, onDisagree);
			e.target.removeEventListener(MouseEvent.CLICK, onAgree);
		}
		
		private function onDisagree(e:MouseEvent):void 
		{
			disagreeBut_onClick();
			
			e.target.removeEventListener(MouseEvent.CLICK, onDisagree);
			e.target.removeEventListener(MouseEvent.CLICK, onAgree);
		}
		
		private function agreeBut_onClick():void 
		{
			var evAgree:UIEvent = new UIEvent(UIEvent.CLICKED);
			evAgree.id = "eula-agreed-to-terms";
			dispatchEvent(evAgree);		
		}
		
		private function disagreeBut_onClick():void 
		{
			var evDisagree:UIEvent = new UIEvent(UIEvent.CLICKED);
			evDisagree.id = "eula-disagreed-to-terms";
			dispatchEvent(evDisagree);		
		}
		
		//------------------------------------------
		//runtime shared library - alert_manager.swf
		//------------------------------------------
		
		//decided to handle rsl loading in here instead of using Cairngorm Architecture
		private function loadAlertManagerRSL():void
		{
			var rsl_loader:Loader = new Loader();
			rsl_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, rsl_loader_onComplete);
			rsl_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
			var url:URLRequest = new URLRequest("shared_assets/player/alert_manager.swf");
			rsl_loader.load(url, context);		
		}
		
		private function loader_onIOError(e:IOErrorEvent):void 
		{
			Debugger.traceOut("Alert Manager rsl IOError");
			
			e.target.removeEventListener(Event.COMPLETE, rsl_loader_onComplete);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
		}
		
		private function rsl_loader_onComplete(e:Event):void 
		{
			Debugger.traceOut("Alert Manager rsl loaded");
			alertManager = e.currentTarget.content as MovieClip;
			
			if(showEula){
				var legalStr:String = "Do you agree with the terms and conditions? Do you agree with the terms and conditions? Do you agree with the terms and conditions?";
				alertManager.dialog(this.getAppRoot(), legalStr, "Eula", ["Agree", "Disagree"], onButtonClicked);
			}else {
				alertManager.alert(this.getAppRoot(), "Intel not present on this machine");
			}
			
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
			e.target.removeEventListener(Event.COMPLETE, rsl_loader_onComplete);
		}
		
		private function onButtonClicked(event:Event):void
		{
			trace("You selected " + event.target.name + ".  ");
			if (event.target.name == "Agree") {
				agreeBut_onClick();
			}else {
				disagreeBut_onClick();
			}
		}
		
		/* INTERFACE pcb.views.ILocalise */
		
		public function localiseLabels(localeModel:LocaleModel):void
		{
			legTxt = localeModel.getLabelbyId("eula");
		}
		
	}
	
}